<?php
// +----------------------------------------------------------------------
// | tpapi.
// +----------------------------------------------------------------------
// | FileName: Controller.php
// +----------------------------------------------------------------------
// | Author: King east <1207877378@qq.com>
// +----------------------------------------------------------------------


namespace ke\thinkApi\src;


use ke\thinkApi\src\traits\TraitRoute;
use think\App;
use think\Container;
use think\Db;
use think\db\Query;
use think\exception\ValidateException;
use think\Model;
use think\Request;

/**
 * Class Controller
 * @package ke\thinkApi
 * @property-read integer page
 * @property-read integer limit
 * @property-read integer offset
 * @property-read integer count
 * @property-read Query db
 */
class Controller
{
    use TraitRoute;

    protected $name;

    /**
     * @var Model
     */
    protected $model;

    /**
     * @var App
     */
    protected $app;

    /**
     * @var Request
     */
    protected $request;


    /**
     * @var Setting
     */
    protected $setting;



    public function __construct($name, Setting $setting = null)
    {
        $this->name = $name;

        $this->app = Container::get('app');
        $this->request = $this->app['request'];

        if ($setting) {
            $this->setting = $setting;
        } else {
            $this->setting = new Setting();
        }
    }

    public function __get($name)
    {
        switch ($name) {
            case 'page':
                return $this->request->get('page', 1, 'intval', function ($v) {
                    return max(1, $v);
                });
            case 'offset':
                return ($this->page - 1) * $this->limit;
            case 'limit':
                return $this->request->get('limit', 20, 'intval', function ($v) {
                    return min(100, $v);
                });
            case 'count':
                return $this->request->get('count', false, 'boolval');
            case 'db':
                return Db::name($this->name);
            default:
                break;
        }
    }


    /**
     * 验证数据
     * @access protected
     * @param  array        $data     数据
     * @param  string|array $validate 验证器名或者验证规则数组
     * @param  array        $message  提示信息
     * @param  bool         $batch    是否批量验证
     * @param  mixed        $callback 回调方法（闭包）
     * @return array|string|true
     * @throws ValidateException
     */
    protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
    {
        if (is_array($validate)) {
            $v = $this->app->validate();
            $v->rule($validate);
        } else {
            if (strpos($validate, '.')) {
                // 支持场景
                list($validate, $scene) = explode('.', $validate);
            }
            $v = $this->app->validate($validate);
            if (!empty($scene)) {
                $v->scene($scene);
            }
        }

        // 是否批量验证
        if ($batch || $this->batchValidate) {
            $v->batch(true);
        }

        if (is_array($message)) {
            $v->message($message);
        }

        if ($callback && is_callable($callback)) {
            call_user_func_array($callback, [$v, &$data]);
        }

        if (!$v->check($data)) {
            if ($this->failException) {
                throw new ValidateException($v->getError());
            }
            return $v->getError();
        }

        return true;
    }
}